/**
 * 会员卡接口处理, 话说, 会员卡接口到处都是魔法数字, 维护够坑
 * */
//处理会员说明
export function parseCategoryIntro(category) {
  let arrayData = []
  if (category) {
    let fTitle = '';
    //卡开通方式
    if (category.payType === 1) {
      fTitle = '会员卡直接开通,';
    } else if (category.payType === 2) {
      fTitle = `会员卡付费 ${category.payCost / 100} 元开通,`;
    } else if (category.payType === 3) {
      fTitle = `预存 ${category.rechargeCost / 100} 元可升级到此会员卡,`
      if (category.annualFee) {
        fTitle += `预存金额包含了
        ${category.annualFee / 100} 元年费,`
        if (!category.firstYearFree) {
          fTitle += "在开通成功后从余额扣除,"
        }
      }
    } else {
      fTitle = `会员卡付费 ${category.payCost / 100} 元或预存 ${category.rechargeCost / 100} 元开通,`;
    }
    arrayData.push(fTitle.substring(0, fTitle.length - 1))
    //卡的有效说明
    let validPeriod = '';
    if (category.validPeriod === 0) {
      validPeriod = '会员卡自领取日起永久有效'
    } else {
      validPeriod = `会员卡自领取日起 ${category.validPeriod} 年有效`
    }
    arrayData.push(validPeriod)
    //年费
    let annualFee = '';
    if (category.annualFee / 100) {
      annualFee = `会员卡需要每年缴纳 ${category.annualFee / 100} 元年费,`
      if (category.firstYearFree) {
        annualFee += "开卡免首年年费,";
      }
      arrayData.push(annualFee.substring(0, annualFee.length - 1))
      //差额升级
      if (category.enablePayDifference) {
        arrayData.push("升级卡到该卡时支持差额升级,需支付原会员卡与当前会员卡的开卡费的差额升级")
      }
      //是否自动续费
      let autoRenew = '';
      if (category.autoRenew) {
        autoRenew = `会员卡到期当天如果会员余额充足,则直接从余额自动扣除年费`
        arrayData.push(autoRenew)
      }
      //卡延长时间
      let payAnnual = '';
      payAnnual = `年费自动扣除或者缴纳成功后,会员卡有效期自动延长一年`
      arrayData.push(payAnnual)
    }
    return arrayData;
  } else {
    return [];
  }
}
// 会员日&会员权益格式化数据
export function parsePrivileges(privileges) {
  let rv = [];
  if (privileges && privileges.length > 0) {
    for (let privilege of privileges) {
      let item = { ...privilege }
      item.shop = privilege.shop ? privilege.shop.split(",") : [];
      item.point = privilege.point ? privilege.point / 10 : null;
      item.discount = privilege.discount ? privilege.discount / 10 : null;
      rv.push(item);
    }
    return rv;
  } else {
    return rv;
  }
}
// 会员卡权益
export function parseCategoryRights(category, rechargeAward) {
  let arrayData = []
  if (category && rechargeAward) {
    //折扣
    if (category.categoryDiscount !== 100) {
      arrayData.push({ title: `消费${category.categoryDiscount / 10}折`, titleValue: `${category.categoryDiscount / 10}折`, iconName: 'icon_discount' })
    }
    //消费积分倍数
    if (category.multiplePoints / 10 > 1) {
      arrayData.push({ title: "消费积分", titleValue: `x${category.multiplePoints / 10}倍`, iconName: 'icon_jifend' })
    }
    //额外说明
    if (category.privilege) {
      let privilegeArr = category.privilege.split(/[(\r\n)\n]+/)
      for (let i = 0; i < privilegeArr.length; i++) {
        arrayData.push({ title: `${privilegeArr[i]}`, iconName: 'icon_book' })
      }
    }
    if (category.customBenefit) {
      let arr = JSON.parse(category.customBenefit);
      if (arr.length > 0) {
        arr.forEach(item => {
          if (item.imgUrl) {
            arrayData.push({ title: item.value, iconName: item.imgUrl })
          } else {
            arrayData.push({ title: item.value, iconName: 'icon_book' })
          }
        })
      }
    }
    return arrayData;
  } else {
    console.log("全部权益字段处理错误");
    return [];
  }
}

//充值送券奖励字段处理(rights) flag 为true旧逻辑，false,新逻辑
export function parseRechargeAwardRule(rechargeAward, flag = true) {
  let arrayData = [];
  if (rechargeAward) {

    if (flag) {
      //充值送奖励
      for (let i = 0; i < rechargeAward.length; i++) {
        let balance = 0;
        let giftBalance = 0;
        let couponString = '';
        let calculateWayText = rechargeAward[i].calculateWay == 1 ? '单次充' : '每充满';
        //余额
        if (rechargeAward[i].giftType === 1) {
          if ((rechargeAward[i].giftBalance / 100) > 0) {
            balance = `${calculateWayText}${rechargeAward[i].baseAmount / 100}元,送${rechargeAward[i].giftBalance / 100}元,`
          } else {
            balance = `${calculateWayText}${rechargeAward[i].baseAmount / 100}元,`
          }

        } else {
          if ((rechargeAward[i].giftPercent) > 0) {
            balance = `${calculateWayText}${rechargeAward[i].baseAmount / 100}元,送${rechargeAward[i].giftPercent}%,`
          } else {
            balance = `${calculateWayText}${rechargeAward[i].baseAmount / 100}元,`
          }
        }
        //券
        if (rechargeAward[i].couponAwards.length > 0) {
          for (let j = 0; j < rechargeAward[i].couponAwards.length; j++) {
            let couponItem = rechargeAward[i].couponAwards[j];
            couponString += `送${couponItem.couponName}${couponItem.amount}张,`
          }
        }
        //积分
        giftBalance = rechargeAward[i].giftPoints ? `送${rechargeAward[i].giftPoints}个积分,` : '';
        let andString = balance + couponString + giftBalance
        arrayData.push({ title: andString.substring(0, andString.length - 1), titleValue: `充${rechargeAward[i].baseAmount / 100}即享`, iconName: 'icon_recharge' });
      }
      console.log('arrayData', arrayData)
      return arrayData;
    } else {
      //新逻辑
      for (let i = 0; i < rechargeAward.length; i++) {
        let balance = 0;
        let giftBalance = 0;
        let couponString = '';
        let arr = {
          money: rechargeAward[i].baseAmount / 100,
          rechargeAwardRights: []
        };
        //余额
        if (rechargeAward[i].giftType === 1) {
          if ((rechargeAward[i].giftBalance / 100) > 0) {
            arr.rechargeAwardRights.push({ title: "余额", value: '￥' + rechargeAward[i].giftBalance / 100, iconName: "icon_balance" })
          }
        } else {
          if ((rechargeAward[i].giftPercent) > 0) {
            arr.rechargeAwardRights.push({ title: "余额", value: '￥' + rechargeAward[i].giftPercent / 100 + '%', iconName: "icon_balance" })
          }
        }
        //券
        if (rechargeAward[i].couponAwards.length > 0) {
          for (let j = 0; j < rechargeAward[i].couponAwards.length; j++) {
            let couponItem = rechargeAward[i].couponAwards[j];
            let title = couponItem.couponName;
            let iconName = couponItem.couponType;
            let value = "x" + couponItem.amount
            if (couponItem.couponType == "CASH" || couponItem.couponType == "cash" || couponItem.couponType == "cash_wx_pay" || couponItem.couponType == "cash_wx_busi") {
              if (couponItem.denomination > 0) { title = `${couponItem.denomination / 100}元代金券`; iconName = "CASH" }
            } else if (couponItem.couponType == "DISCOUNT" || couponItem.couponType == "discount" || couponItem.couponType == "discount_wx_busi") {
              if (couponItem.denomination > 0) { title = `${couponItem.denomination / 10}折券`; iconName = "DISCOUNT" }
            } else if (couponItem.couponType == 'exchange') {
              iconName = "GENERAL"
            } else if (couponItem.couponType == "times") {
              iconName = "icon_cicard";
              title = `${couponItem.denomination / 100}次卡`
            } else if (couponItem.couponType == 'gift') {
              iconName = "icon_lipincard";
              title = `${couponItem.denomination / 100}元卡`
              value = '￥' + couponItem.denomination / 100;
            }
            arr.rechargeAwardRights.push({ title: title, value: value, iconName: iconName })
          }
        }
        //积分
        if (rechargeAward[i].giftPoints > 0) {
          arr.rechargeAwardRights.push({ title: "积分", value: 'x' + rechargeAward[i].giftPoints, iconName: "icon_balance" })
        }
        // giftBalance = rechargeAward[i].giftPoints ? `送${rechargeAward[i].giftPoints}个积分,` : '';
        // let andString = balance + couponString + giftBalance
        arrayData.push({ ...arr });
      }
      console.log("新的arrayData", arrayData)
      return arrayData
    }
  } else {
    console.log("充值送券奖励字段处理错误");
    return arrayData;
  }
}

//flag 是新加的参数 在不影响原有的逻辑情况下，新增新的需求要求
export function parseOpenCardGift(category, openCardCouponAward, flag = false) {
  let openCardGiftArr = [];
  //卡自带
  if (category.giftBalance) {
    openCardGiftArr.push({ title: "余额", value: '￥' + category.giftBalance / 100, aFix: "元", iconName: "icon_balance" })
  }
  if (category.giftPoints) {
    openCardGiftArr.push({ title: "积分", value: category.giftPoints, aFix: "个", iconName: "icon_jifen" })
  }
  /*券赠送*/
  if (openCardCouponAward.length > 0) {
    if (!flag) {
      openCardCouponAward.forEach(value => {
        openCardGiftArr.push({ title: value.couponName, value: 'X' + value.amount, aFix: "张", iconName: value.couponType })
      })
    } else {
      openCardCouponAward.forEach(value => {
        let title = value.couponName;
        let iconName = value.couponType;
        //兼容卡券2.0
        if (value.couponType == "CASH" || value.couponType == "cash" || value.couponType == "cash_wx_pay" || value.couponType == "cash_wx_busi") {
          if (value.denomination > 0) { title = `${value.denomination / 100}元代金券`; iconName = "CASH" }
        } else if (value.couponType == "DISCOUNT" || value.couponType == "discount" || value.couponType == "discount_wx_busi") {
          if (value.denomination > 0) { title = `${value.denomination / 10}折券`; iconName = "DISCOUNT" }
        } else if (value.couponType == 'exchange') {
          iconName = "GENERAL"
        } else if (value.couponType == "times") {
          iconName = "icon_cicard"
        } else if (value.couponType == 'gift') {
          iconName = "icon_lipincard"
        }
        openCardGiftArr.push({ title: title, value: 'X' + value.amount, aFix: "张", iconName: iconName })
      })
    }
  }
  console.log("openCardGiftArr", openCardGiftArr)
  return openCardGiftArr
}


export function rechargeAwardDillMap(rechargeAward) {
  let arrayData = []
  if (rechargeAward) {
    //充值送奖励
    for (let i = 0; i < rechargeAward.length; i++) {
      let rechargeMap = {
        rechargeMoney: null,
        sendArray: [],
      }
      if (rechargeAward[i].giftType && (rechargeAward[i].giftBalance || rechargeAward[i].giftPercent)) {
        let balance = null;
        //余额
        if (rechargeAward[i].giftType === 1) {
          balance = `￥${rechargeAward[i].giftBalance / 100}`
        } else {
          balance = `${rechargeAward[i].giftPercent}%`
        }
        rechargeMap.sendArray.push({
          title: '余额返现',
          value: balance,
          iconName: "icon_recharge"
        })
      }

      //券
      if (rechargeAward[i].couponAwards.length > 0) {
        for (let j = 0; j < rechargeAward[i].couponAwards.length; j++) {
          let couponItem = rechargeAward[i].couponAwards[j];
          rechargeMap.sendArray.push({
            title: couponItem.couponName,
            value: couponItem.amount + '张',
            iconName: 'icon_recharge'
          })
        }
      }
      //积分
      if (rechargeAward[i].giftPoints) {
        rechargeMap.sendArray.push({
          title: '积分',
          value: rechargeAward[i].giftPoints + '个',
          iconName: "icon_recharge"
        })
      }
      //处理完数据后赋值
      rechargeMap.rechargeMoney = rechargeAward[i].baseAmount / 100
      arrayData.push(rechargeMap);
    }
    return arrayData;
  } else {
    return arrayData;
  }
}
